package com.itheima.algorithm.heap;

/**
 * 可以扩容的 heap, max 用于指定是大顶堆还是小顶堆
 */
public class Heap {
    int[] array;
    int size;
    boolean maxFlag;

    public Heap(int capacity, boolean maxFlag) {
        this.array = new int[capacity];
        this.maxFlag = maxFlag;
        heapify();
    }

    /**
     * 建堆
     */
    public void heapify() {
        for (int i = size / 2 - 1; i >= 0; i--) {
            down(i);
        }
    }

    /**
     * 将 parent 索引处3的元素下潜：与两个孩子较大者交换，直至没孩子或孩子没它大
     * @param parent
     */
    public void down(int parent) {
        int left = parent * 2 + 1;
        int right = left + 1;
        int maxOrMin = parent;
        if ((left < size) && (maxFlag ? array[maxOrMin] < array[left] : array[maxOrMin] > array[left])) {
            maxOrMin = left;
        }
        if ((right < size) && (maxFlag ? array[maxOrMin] < array[right] : array[maxOrMin] > array[right])) {
            maxOrMin = right;
        }
        if (maxOrMin != parent) { // 找到了更大的孩子
            swap(maxOrMin, parent);
            down(maxOrMin);
        }
    }

    /**
     * 获取堆顶元素
     * @return 堆顶元素
     */
    public int peek() {
        return array[0];
    }

    /**
     * 获取堆顶元素
     * @return 堆顶元素
     */
    public int poll() {
        int value = array[0];
        swap(0, size-1);
        size--;
        down(0);
        array[size] = 0;
        return value;
    }

    /**
     * 替换堆顶元素
     * @param replaced - 新元素
     */
    public void replace(int replaced) {
        array[0] = replaced;
        down(0);
    }

    /**
     * 堆的尾部添加元素
     * @param offered - 被添加元素值
     * @return 是否添加成功
     */
    public void offer(int offered) {
        if (size == array.length) {
            grow();
        }
        up(offered);
        size++;
    }

    public void grow() {
        int capacity = size + (size >> 1);
        int[] newArrary = new int[capacity];
        System.arraycopy(array, 0, newArrary, 0, size);
        array = newArrary;
    }

    /**
     * 将 offered 元素上浮：直至 offered 小鱼父元素或到堆顶
     * @param offered
     */
    public void up(int offered) {
        int child = size;
        while (child > 0) {
            int parent = (child - 1) / 2;
            boolean cmp = maxFlag ? offered > array[parent] : offered < array[parent];
            if (cmp) {
                array[child] = array[parent];
            } else {
                break;
            }
            child = parent;
        }
        array[child] = offered;
    }


    /**
     * 交换两个索引处的元素
     * @param i
     * @param j
     */
    public void swap(int i, int j) {
        int t = array[i];
        array[i] = array[j];
        array[j] = t;
    }

    public boolean isFull() {
        return size == array.length;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < size; i++) {
            sb.append(array[i]);
            if (i != size - 1) {
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }
}